home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-13 / mewin10s.zip / MAIN.C < prev    next >
C/C++ Source or Header  |  1992-04-01  |  26KB  |  1,114 lines

  1. /*
  2.  *    MicroEMACS 3.11
  3.  *        written by Daniel M. Lawrence
  4.  *        based on code by Dave G. Conroy.
  5.  *
  6.  *    (C)Copyright 1988,1989,1990,1991 by Daniel M. Lawrence
  7.  *    MicroEMACS 3.11 can be copied and distributed freely for any
  8.  *    non-commercial purposes. MicroEMACS 3.11 can only be incorporated
  9.  *    into commercial software with the permission of the current author.
  10.  *
  11.  * This file contains the main driving routine, and some keyboard processing
  12.  * code, for the MicroEMACS screen editor.
  13.  *
  14.  */
  15.  
  16. #include    <stdio.h>
  17.  
  18. /* make global definitions not external */
  19. #define maindef
  20.  
  21. #include    "estruct.h"    /* global structures and defines */
  22. #include    "eproto.h"    /* variable prototype definitions */
  23. #include    "efunc.h"    /* function declarations and name table */
  24. #include    "edef.h"    /* global definitions */
  25. #include    "elang.h"    /* human language definitions */
  26. #include    "ebind.h"    /* default key bindings */
  27.  
  28. /* for many different systems, increase the default stack space */
  29.  
  30. #if    MSDOS && MSC
  31. unsigned _stackavail = 20000;
  32. #endif
  33.  
  34. #if    MSDOS && LATTICE
  35. unsigned _stack = 20000;
  36. #endif
  37.  
  38. #if    MSDOS && (DTL | ZTC)
  39. int    _okbigbuf = 0;        /* Only allocate memory when needed.*/
  40. int    _stack = 20000;     /* Reset the ol' stack size.*/
  41. #endif
  42.  
  43. #if    TOS && MWC
  44. long _stksize = 20000L;     /* reset stack size (must be even) */
  45. #endif
  46.  
  47. #if    MSDOS && AZTEC
  48. int _STKSIZ = 20000/16;     /* stack size in paragraphs */
  49. int _STKRED = 1024;        /* stack checking limit */
  50. int _HEAPSIZ = 4096/16;     /* (in paragraphs) */
  51. /*int _STKLOW = 0;        default is stack above heap (small only) */
  52. #endif
  53.  
  54. #if    MSDOS && TURBO
  55. extern unsigned int _stklen = 10000;
  56. #endif
  57.  
  58. /*    make VMS happy...    */
  59.  
  60. #if    VMS
  61. #include    <ssdef.h>
  62. #define GOOD    (SS$_NORMAL)
  63. #endif
  64.  
  65. #ifndef GOOD
  66. #define GOOD    0
  67. #endif
  68.  
  69. /*
  70.     This is the primary entry point that is used by command line
  71.     invocation, and by applications that link with microemacs in
  72.     such a way that each invocation of Emacs is a fresh environment.
  73.  
  74.     There is another entry point in VMS.C that is used when
  75.     microemacs is "linked" (In quotes, because it is a run-time link
  76.     rather than a link-time link.) with applications that wish Emacs
  77.     to preserve it's context across invocations.  (For example,
  78.     EMACS.RC is only executed once per invocation of the
  79.     application, instead of once per invocation of Emacs.)
  80.  
  81.     Note that re-entering an Emacs that is saved in a kept
  82.     subprocess would require a similar entrypoint.
  83. */
  84.  
  85. #if    CALLED
  86. emacs(argc, argv)
  87. #else
  88. main(argc, argv)
  89. #endif
  90.  
  91. int argc;    /* # of arguments */
  92. char *argv[];    /* argument strings */
  93.  
  94. {
  95.     register int status;
  96.  
  97.     /* Initialize the editor */
  98.     eexitflag = FALSE;
  99. #if     !WINDOW_MSWIN
  100.     vtinit();        /* Terminal */
  101. #endif
  102.  
  103.     if (eexitflag)
  104.         goto abortrun;
  105.     edinit(mainbuf);      /* Buffers, windows, screens */
  106.  
  107.     varinit();        /* user variables */
  108.     initchars();        /* character set definitions */
  109.  
  110.     /* Process the command line and let the user edit */
  111. #if    VMS
  112.     expandargs(&argc, &argv);    /* expand VMS wildcards.*/
  113. #endif
  114.     dcline(argc, argv, TRUE);
  115.     status = editloop();
  116. abortrun:
  117.     vttidy();
  118. #if    CLEAN
  119.     clean();
  120. #endif
  121. #if    CALLED
  122.     return(status);
  123. #else
  124.     exit(status);
  125. #endif
  126. }
  127.  
  128. #if    CLEAN
  129. /*
  130.     On some primitive operation systems, and when emacs is used as
  131.     a subprogram to a larger project, emacs needs to de-alloc its
  132.     own used memory, otherwise we just exit.
  133. */
  134.  
  135. PASCAL NEAR clean()
  136. {
  137.     register BUFFER *bp;    /* buffer list pointer */
  138.     register SCREEN *scrp;    /* ptr to screen to dump */
  139.  
  140.     /* first clean up the screens */
  141.     scrp = first_screen;
  142.     while (scrp) {
  143.         first_screen = sp->s_next_screen;
  144.         free_screen(scrp);
  145.         scrp = first_screen;
  146.     }
  147.     wheadp = NULL;
  148.  
  149.     /* then the buffers */
  150.     bp = bheadp;
  151.     while (bp) {
  152.         bp->b_nwnd = 0;
  153.         bp->b_flag = 0; /* don't say anything about a changed buffer! */
  154.         zotbuf(bp);
  155.         bp = bheadp;
  156.     }
  157.  
  158.     /* and the kill buffers */
  159.     clear_ring();
  160.  
  161.     /* clear some search variables */
  162. #if    MAGIC
  163.     mcclear();
  164.     rmcclear();
  165. #endif
  166.     if (patmatch != NULL) {
  167.         free(patmatch);
  168.         patmatch = NULL;
  169.     }
  170.  
  171.     /* dealloc the user variables */
  172.     varclean();
  173.  
  174.     /* and the video buffers */
  175.     vtfree();
  176. }
  177. #endif
  178.  
  179. /*    Process a command line.   May be called any time.    */
  180.  
  181. PASCAL NEAR dcline(argc, argv, firstflag)
  182.  
  183. int argc;
  184. char *argv[];
  185. int firstflag;    /* is this the first time in? */
  186.  
  187. {
  188.     register BUFFER *bp;        /* temp buffer pointer */
  189.     register int    firstfile;    /* first file flag */
  190.     register int    carg;        /* current arg to scan */
  191.     register int    startflag;    /* startup executed flag */
  192.     BUFFER *firstbp = NULL;     /* ptr to first buffer in cmd line */
  193.     int viewflag;            /* are we starting in view mode? */
  194.     int gotoflag;            /* do we need to goto a line at start? */
  195.     int gline;            /* if so, what line? */
  196.     int searchflag;         /* Do we need to search at start? */
  197.     int errflag;            /* C error processing? */
  198.     VDESC vd;            /* variable num/type */
  199.     char bname[NBUFN];        /* buffer name of file to read */
  200.  
  201. #if    CRYPT
  202.     int cryptflag;            /* encrypting on the way in? */
  203.     char ekey[NPAT];        /* startup encryption key */
  204. #endif
  205.     CONST NOSHARE extern *pathname[]; /* startup file path/name array */
  206.  
  207.     viewflag = FALSE;    /* view mode defaults off in command line */
  208.     gotoflag = FALSE;    /* set to off to begin with */
  209.     searchflag = FALSE;    /* set to off to begin with */
  210.     firstfile = TRUE;    /* no file to edit yet */
  211.     startflag = FALSE;    /* startup file not executed yet */
  212.     errflag = FALSE;    /* not doing C error parsing */
  213.     exec_error = FALSE;    /* no macro error pending */
  214. #if    CRYPT
  215.     cryptflag = FALSE;    /* no encryption by default */
  216. #endif
  217.     disphigh = FALSE;    /* don't escape high bit characters */
  218.     lterm[0] = 0;        /* standard line terminators */
  219.  
  220.     /* Parse a command line */
  221.     for (carg = 1; carg < argc; ++carg) {
  222.  
  223.         /* Process Switches */
  224. #if WMCS
  225.         if (argv[carg][0] == ':') {
  226. #else
  227.         if (argv[carg][0] == '-') {
  228. #endif
  229.             switch (argv[carg][1]) {
  230.                 /* Process Startup macroes */
  231.                 case 'c':    /* -c for changable file */
  232.                 case 'C':
  233.                     viewflag = FALSE;
  234.                     break;
  235.                 case 'e':    /* -e process error file */
  236.                 case 'E':
  237.                     errflag = TRUE;
  238.                     break;
  239.                 case 'g':    /* -g for initial goto */
  240.                 case 'G':
  241.                     gotoflag = TRUE;
  242.                     gline = asc_int(&argv[carg][2]);
  243.                     break;
  244.                 case 'i':    /* -i<var> <value> set an initial */
  245.                 case 'I':    /* value for a variable */
  246.                     bytecopy(bname, &argv[carg][2], NVSIZE);
  247.                     findvar(bname, &vd, NVSIZE + 1);
  248.                     if (vd.v_type == -1) {
  249.                         mlwrite(TEXT52, bname);
  250. /*                            "%%No such variable as '%s'" */
  251.                         break;
  252.                     }
  253.                     svar(&vd, argv[++carg]);
  254.                     break;
  255. #if    CRYPT
  256.                 case 'k':    /* -k<key> for code key */
  257.                 case 'K':
  258.                     cryptflag = TRUE;
  259.                     strcpy(ekey, &argv[carg][2]);
  260.                     break;
  261. #endif
  262.                 case 'r':    /* -r restrictive use */
  263.                 case 'R':
  264.                     restflag = TRUE;
  265.                     break;
  266.                 case 's':    /* -s for initial search string */
  267.                 case 'S':
  268.                     searchflag = TRUE;
  269.                     bytecopy(pat,&argv[carg][2],NPAT);
  270.                     setjtable();
  271.                     break;
  272.                 case 'v':    /* -v for View File */
  273.                 case 'V':
  274.                     viewflag = TRUE;
  275.                     break;
  276.                 default:    /* unknown switch */
  277.                     /* ignore this for now */
  278.                     break;
  279.             }
  280.  
  281.         } else if (argv[carg][0]== '@') {
  282.  
  283.             /* Process Startup macroes */
  284.             if (startup(&argv[carg][1]) == TRUE)
  285.                 /* don't execute emacs.rc */
  286.                 startflag = TRUE;
  287.  
  288.         } else {
  289.  
  290.             /* Process an input file */
  291.  
  292.             /* set up a buffer for this file */
  293.             makename(bname, argv[carg]);
  294.             unqname(bname);
  295.  
  296.             /* set this to inactive */
  297.             bp = bfind(bname, TRUE, 0);
  298.             strcpy(bp->b_fname, argv[carg]);
  299. #if     WINDOW_MSWIN
  300.             fullpathname (bp->b_fname, NFILEN);
  301. #endif
  302.             bp->b_active = FALSE;
  303.             if (firstfile) {
  304.                 firstbp = bp;
  305.                 firstfile = FALSE;
  306.             }
  307.  
  308.             /* set the modes appropriatly */
  309.             if (viewflag)
  310.                 bp->b_mode |= MDVIEW;
  311. #if    CRYPT
  312.             if (cryptflag) {
  313.                 bp->b_mode |= MDCRYPT;
  314.                 crypt((char *)NULL, 0);
  315.                 crypt(ekey, strlen(ekey));
  316.                 bytecopy(bp->b_key, ekey, NPAT);
  317.             }
  318. #endif
  319.         }
  320.     }
  321.  
  322.     /* if we are C error parsing... run it! */
  323.     if (errflag) {
  324.         if (startup("error.cmd") == TRUE)
  325.             startflag = TRUE;
  326.     }
  327.  
  328.     /* if invoked with no other startup files,
  329.        run the system startup file here */
  330.     if (firstflag && startflag == FALSE)
  331.         startup("");
  332.  
  333.     /* if there are any files to read, read the first one! */
  334.     if (firstflag) {
  335.         bp = bfind(mainbuf, FALSE, 0);
  336.         if (firstfile == FALSE && (gflags & GFREAD)) {
  337.             swbuffer(firstbp);
  338.             curbp->b_mode |= gmode;
  339.             update(TRUE);
  340.             mlwrite(lastmesg);
  341.             zotbuf(bp);
  342.         } else
  343.             bp->b_mode |= gmode;
  344.     } else {
  345.         swbuffer(firstbp);
  346.         curbp->b_mode |= gmode;
  347.         update(TRUE);
  348.         mlwrite(lastmesg);
  349.     }
  350.  
  351.  
  352.     /* Deal with startup gotos and searches */
  353.     if (gotoflag && searchflag) {
  354.         update(FALSE);
  355.         mlwrite(TEXT101);
  356. /*            "[Can not search and goto at the same time!]" */
  357.     }
  358.     else if (gotoflag) {
  359.         if (gotoline(TRUE,gline) == FALSE) {
  360.             update(FALSE);
  361.             mlwrite(TEXT102);
  362. /*                "[Bogus goto argument]" */
  363.         }
  364.     } else if (searchflag) {
  365.         if (forwhunt(FALSE, 0) == FALSE)
  366.             update(FALSE);
  367.     }
  368.  
  369. }
  370.  
  371. #if     WINDOW_MSWIN
  372. #define GETBASEKEY getbasekey
  373. static int PASCAL NEAR getbasekey()
  374. {
  375.     register int c;
  376.  
  377.     notquiescent = -1;  /* will be <= 0 only if getkey() is called
  378.                directly from editloop(). This is used to
  379.                restrict some windows-specific actions
  380.                (menus, sizing, etc...) when not called from
  381.                the lowest level of the editor */
  382.     c = getkey();
  383.     notquiescent = 1;
  384.     return c;
  385. }
  386. #else
  387. #define GETBASEKEY getkey
  388. #endif
  389.  
  390. /*
  391.     This is called to let the user edit something.    Note that if you
  392.     arrange to be able to call this from a macro, you will have
  393.     invented the "recursive-edit" function.
  394. */
  395.  
  396. PASCAL NEAR editloop()
  397.  
  398. {
  399.     register int c;        /* command character */
  400.     register int f;     /* default flag */
  401.     register int n;     /* numeric repeat count */
  402.     register int mflag;    /* negative flag on repeat */
  403.     register int basec;    /* c stripped of meta character */
  404.     register int oldflag;    /* old last flag value */
  405.     char time[6];        /* current display time */
  406.  
  407.     /* setup to process commands */
  408.     lastflag = 0;        /* Fake last flags.    */
  409.  
  410. loop:
  411.     /* if a macro error is pending, wait for a character */
  412.     if (exec_error) {
  413. #if     WINDOW_MSWIN
  414.                 mlhistory();
  415. #else
  416.         mlforce(TEXT227);
  417. /*            "\n--- Press any key to Continue ---" */
  418.         tgetc();
  419. #endif
  420.         sgarbf = TRUE;
  421.         update(FALSE);
  422.         mlferase();
  423.         exec_error = FALSE;
  424.     }
  425.  
  426.     /* if we were called as a subroutine and want to leave, do so */
  427.     if (eexitflag)
  428.         return(eexitval);
  429.  
  430.     /* execute the "command" macro...normally null */
  431.     oldflag = lastflag;    /* preserve lastflag through this */
  432.     execkey(&cmdhook, FALSE, 1);
  433.     lastflag = oldflag;
  434.  
  435. #if    VMS
  436.     if (pending_msg) {
  437.         makelit(brdcstbuf);
  438.         mlwrite(brdcstbuf);
  439.         pending_msg = FALSE;
  440.     }
  441. #endif
  442.  
  443.     /* update time on the bottom modeline? */
  444.     if (timeflag)
  445. #if TYPEAH || WINDOW_MSWIN
  446.             if (!typahead())
  447. #endif
  448.             {
  449.         getdtime(time);
  450.         if (strcmp(lasttime, time) != 0)
  451.             upmode();
  452.     }
  453.  
  454.     /* update position on current modeline? */
  455.     if (posflag)
  456. #if TYPEAH || WINDOW_MSWIN
  457.             if (!typahead())
  458. #endif
  459.         upmode();
  460.  
  461.     /* Fix up the screen    */
  462.     update(FALSE);
  463.  
  464.     /* get the next command from the keyboard */
  465.     discmd = TRUE;
  466.     disinp = TRUE;
  467.     c = GETBASEKEY();
  468.  
  469.     /* if there is something on the command line, clear it */
  470.     if (mpresf != FALSE) {
  471.         mlerase();
  472.         update(FALSE);
  473.     }
  474.  
  475.     /* override the arguments if prefixed */
  476.     if (prefix) {
  477.         if (islower(c & 255))
  478.             c = (c & ~255) | upperc(c & 255);
  479.         c |= prefix;
  480.         f = predef;
  481.         n = prenum;
  482.         prefix = 0;
  483.     } else {
  484.         f = FALSE;
  485.         n = 1;
  486.     }
  487.  
  488.     /* do META-# processing if needed */
  489.  
  490.     basec = c & ~META;        /* strip meta char off if there */
  491.     if ((c & META) && ((basec >= '0' && basec <= '9') || basec == '-') &&
  492.         (getbind(c) == NULL)) {
  493.         f = TRUE;        /* there is a # arg */
  494.         n = 0;            /* start with a zero default */
  495.         mflag = 1;        /* current minus flag */
  496.         c = basec;        /* strip the META */
  497.         while ((c >= '0' && c <= '9') || (c == '-')) {
  498.             if (c == '-') {
  499.                 /* already hit a minus or digit? */
  500.                 if ((mflag == -1) || (n != 0))
  501.                     break;
  502.                 mflag = -1;
  503.             } else {
  504.                 n = n * 10 + (c - '0');
  505.             }
  506.             if ((n == 0) && (mflag == -1))    /* lonely - */
  507.                 mlwrite("Arg:");
  508.             else
  509.                 mlwrite("Arg: %d",n * mflag);
  510.  
  511.             c = GETBASEKEY();    /* get the next key */
  512.         }
  513.         n = n * mflag;    /* figure in the sign */
  514.     }
  515.  
  516.     /* do ^U repeat argument processing */
  517.  
  518.     if (c == reptc) {           /* ^U, start argument   */
  519.         f = TRUE;
  520.         n = 4;                /* with argument of 4 */
  521.         mflag = 0;            /* that can be discarded. */
  522.         mlwrite("Arg: 4");
  523.         while ((c=GETBASEKEY()) >='0' && c<='9' || c==reptc || c=='-') {
  524.             if (c == reptc)
  525.                 if ((n > 0) == ((n*4) > 0))
  526.                     n = n*4;
  527.                 else
  528.                     n = 1;
  529.             /*
  530.              * If dash, and start of argument string, set arg.
  531.              * to -1.  Otherwise, insert it.
  532.              */
  533.             else if (c == '-') {
  534.                 if (mflag)
  535.                     break;
  536.                 n = 0;
  537.                 mflag = -1;
  538.             }
  539.             /*
  540.              * If first digit entered, replace previous argument
  541.              * with digit and set sign.  Otherwise, append to arg.
  542.              */
  543.             else {
  544.                 if (!mflag) {
  545.                     n = 0;
  546.                     mflag = 1;
  547.                 }
  548.                 n = 10*n + c - '0';
  549.             }
  550.             mlwrite("Arg: %d", (mflag >=0) ? n : (n ? -n : -1));
  551.         }
  552.         /*
  553.          * Make arguments preceded by a minus sign negative and change
  554.          * the special argument "^U -" to an effective "^U -1".
  555.          */
  556.         if (mflag == -1) {
  557.             if (n == 0)
  558.                 n++;
  559.             n = -n;
  560.         }
  561.     }
  562.  
  563.     /* and execute the command */
  564.     execute(c, f, n);
  565.     goto loop;
  566. }
  567.  
  568. /*
  569.  * Initialize all of the buffers, windows and screens. The buffer name is
  570.  * passed down as an argument, because the main routine may have been told
  571.  * to read in a file by default, and we want the buffer name to be right.
  572.  */
  573.  
  574. PASCAL NEAR edinit(bname)
  575.  
  576. char bname[];    /* name of buffer to initialize */
  577.  
  578. {
  579.     register BUFFER *bp;
  580.     register int index;
  581.  
  582.     /* init the kill ring */
  583.     for (index = 0; index < NRING; index++) {
  584.         kbufp[index] = (KILL *)NULL;
  585.         kbufh[index] = (KILL *)NULL;
  586.         kskip[index] = 0;
  587.         kused[index] = KBLOCK;
  588.     }
  589.     kill_index = 0;
  590.  
  591.     /* initialize some important globals */
  592.  
  593.     readhook.k_ptr.fp = nullproc;    /* set internal hooks to OFF */
  594.     readhook.k_type = BINDFNC;
  595.     wraphook.k_ptr.fp = wrapword;
  596.     wraphook.k_type = BINDFNC;
  597.     cmdhook.k_ptr.fp = nullproc;
  598.     cmdhook.k_type = BINDFNC;
  599.     writehook.k_ptr.fp = nullproc;
  600.     writehook.k_type = BINDFNC;
  601.     bufhook.k_ptr.fp = nullproc;
  602.     bufhook.k_type = BINDFNC;
  603.     exbhook.k_ptr.fp = nullproc;
  604.     exbhook.k_type = BINDFNC;
  605.  
  606.     /* allocate the first buffer */
  607.     bp = bfind(bname, TRUE, 0);        /* First buffer     */
  608.     blistp = bfind("[Buffers]", TRUE, BFINVS); /* Buffer list buffer    */
  609.     slistp = bfind("[Screens]", TRUE, BFINVS); /* Buffer list buffer    */
  610.     if (bp==NULL || blistp==NULL)
  611.         meexit(1);
  612.  
  613.     /* and allocate the default screen */
  614.     first_screen = (SCREEN *)NULL;
  615.     init_screen("MAIN", bp);
  616.     if (first_screen == (SCREEN *)NULL)
  617.         meexit(1);
  618.  
  619.     /* set the current default screen/buffer/window */
  620.     curbp = bp;
  621.     curwp = wheadp = first_screen->s_cur_window = first_screen->s_first_window;
  622. }
  623.  
  624. /*
  625.  * This is the general command execution routine. It handles the fake binding
  626.  * of all the keys to "self-insert". It also clears out the "thisflag" word,
  627.  * and arranges to move it to the "lastflag", so that the next command can
  628.  * look at it. Return the status of command.
  629.  */
  630. PASCAL NEAR execute(c, f, n)
  631.  
  632. int c;        /* key to execute */
  633. int f;        /* prefix argument flag */
  634. int n;        /* prefix value */
  635.  
  636. {
  637.     register int status;
  638.     KEYTAB *key;        /* key entry to execute */
  639. #if    DBCS
  640.     int schar;        /* second key in 2 byte sequence */
  641. #endif
  642.  
  643. #if    WINDOW_MSWIN
  644.     /* if it is a menu command, go for it... */
  645.     if ((c & MENU) == MENU) {
  646.         thisflag = 0;
  647.         status = execmenu(f, n);
  648.         lastflag = thisflag;
  649.         return(status);
  650.     }
  651. #endif
  652.     /* if the keystroke is a bound function...do it */
  653.     key = getbind(c);
  654.     if (key != NULL) {
  655.  
  656.         /* Don't reset the function type flags on a prefix */
  657.         if ((key->k_type == BINDFNC) &&
  658.             ((key->k_ptr.fp == meta) || (key->k_ptr.fp == cex)))
  659.             status = execkey(key, f, n);
  660.         else {
  661.             thisflag = 0;
  662.             status = execkey(key, f, n);
  663.             lastflag = thisflag;
  664.         }
  665.  
  666.         return(status);
  667.     }
  668.  
  669.     /*
  670.      * If a space was typed, fill column is defined, the argument is non-
  671.      * negative, wrap mode is enabled, and we are now past fill column,
  672.      * and we are not read-only, perform word wrap.
  673.      */
  674.     if (c == ' ' && (curwp->w_bufp->b_mode & MDWRAP) && fillcol > 0 &&
  675.         n >= 0 && getccol(FALSE) > fillcol &&
  676.         (curwp->w_bufp->b_mode & MDVIEW) == FALSE)
  677.         execkey(&wraphook, FALSE, 1);
  678.  
  679.     if ((c>=0x20 && c<=0xFF)) {    /* Self inserting.    */
  680.         if (n <= 0) {            /* Fenceposts.        */
  681.             lastflag = 0;
  682.             return(n<0 ? FALSE : TRUE);
  683.         }
  684.         thisflag = 0;            /* For the future.    */
  685.  
  686.         /* replace or overwrite mode, not at the end of a string */
  687.         if (curwp->w_bufp->b_mode & (MDREPL | MDOVER) &&
  688.             curwp->w_doto < curwp->w_dotp->l_used) {
  689.  
  690.             /* if we are in replace mode, or
  691.                (next char is not a tab or we are at a tab stop) */
  692.             if (curwp->w_bufp->b_mode & MDREPL ||
  693.                 (lgetc(curwp->w_dotp, curwp->w_doto) != '\t' ||
  694.                 getccol(FALSE) % tabsize == (tabsize - 1)))
  695. /*                (curwp->w_doto) % tabsize == (tabsize - 1)))
  696.                 ^^^^^^^^^^^^^ these needs to be the column,
  697.             DML          not the offset! */
  698.                         ldelete(1L, FALSE);
  699.         }
  700.  
  701.         /* do the appropriate insertion */
  702.         if (c == '}' && (curbp->b_mode & MDCMOD) != 0)
  703.             status = insbrace(n, c);
  704.         else if (c == '#' && (curbp->b_mode & MDCMOD) != 0)
  705.             status = inspound();
  706. #if    DBCS
  707.         else if (is2char(c)) {
  708.             schar = getkey();
  709.             status = TRUE;
  710.             while (n--) {
  711.                 if (linsert(1, c) == FALSE)
  712.                     status = FALSE;
  713.                 if (linsert(1, schar) == FALSE)
  714.                     status = FALSE;
  715.             }
  716.         }
  717. #endif
  718.  
  719.         else
  720.             status = linsert(n, c);
  721.  
  722.         /* check for CMODE fence matching */
  723.         if ((c == '}' || c == ')' || c == ']') &&
  724.                 (curbp->b_mode & MDCMOD) != 0)
  725.             fmatch(c);
  726.  
  727.         /* check auto-save mode */
  728.         if (curbp->b_mode & MDASAVE)
  729.             if (--gacount == 0) {
  730.                 /* and save the file if needed */
  731.                 upscreen(FALSE, 0);
  732.                 filesave(FALSE, 0);
  733.                 gacount = gasave;
  734.             }
  735.  
  736.         lastflag = thisflag;
  737.         return(status);
  738.     }
  739.     TTbeep();
  740.     mlwrite(TEXT19);        /* complain        */
  741. /*        "[Key not bound]" */
  742.     lastflag = 0;                /* Fake last flags.    */
  743.     return(FALSE);
  744. }
  745.  
  746. /*
  747.     Fancy quit command, as implemented by Norm. If the any buffer
  748. has changed do a write on that buffer and exit emacs, otherwise simply
  749. exit.
  750. */
  751.  
  752. PASCAL NEAR quickexit(f, n)
  753.  
  754. int f,n;    /* prefix flag and argument */
  755.  
  756. {
  757.     register BUFFER *bp;    /* scanning pointer to buffers */
  758.     register BUFFER *oldcb; /* original current buffer */
  759.     register int status;
  760.  
  761.     oldcb = curbp;                /* save in case we fail */
  762.  
  763. #if    TIPC
  764.     mlwrite("\n\n");
  765. #endif
  766.     bp = bheadp;
  767.     while (bp != NULL) {
  768.         if ((bp->b_flag&BFCHG) != 0    /* Changed.        */
  769.         && (bp->b_flag&BFINVS) == 0) {    /* Real.        */
  770.             curbp = bp;        /* make that buffer cur */
  771.             mlwrite(TEXT103,bp->b_fname);
  772. /*                "[Saving %s]" */
  773.             mlwrite("\n");
  774.             if ((status = filesave(f, n)) != TRUE) {
  775.                 curbp = oldcb;    /* restore curbp */
  776.                 return(status);
  777.             }
  778.         }
  779.     bp = bp->b_bufp;            /* on to the next buffer */
  780.     }
  781.     quit(f, n);                /* conditionally quit    */
  782.     return(TRUE);
  783. }
  784.  
  785. /*
  786.  * Quit command. If an argument, always quit. Otherwise confirm if a buffer
  787.  * has been changed and not written out. Normally bound to "C-X C-C".
  788.  */
  789.  
  790. PASCAL NEAR quit(f, n)
  791.  
  792. int f,n;    /* prefix flag and argument */
  793.  
  794. {
  795.     register int status;    /* return status */
  796.  
  797.     if (f != FALSE        /* Argument forces it.    */
  798.     || anycb() == FALSE    /* All buffers clean or user says it's OK. */
  799.     || (status = mlyesno(TEXT104)) == TRUE) {
  800. /*                 "Modified buffers exist. Leave anyway" */
  801. #if    FILOCK
  802.         if (lockrel() != TRUE) {
  803.             TTputc('\n');
  804.             TTputc('\r');
  805.             TTclose();
  806.             TTkclose();
  807.             status = meexit(1);
  808.         }
  809. #endif
  810.         if (f)
  811.             status = meexit(n);
  812.         else
  813.             status = meexit(GOOD);
  814.     }
  815.     mlerase();
  816.     return(status);
  817. }
  818.  
  819. PASCAL NEAR meexit(status)
  820. int status;    /* return status of emacs */
  821. {
  822.     eexitflag = TRUE;    /* flag a program exit */
  823.     eexitval = status;
  824.  
  825.     /* and now.. we leave and let the main loop kill us */
  826.     return(TRUE);
  827. }
  828.  
  829. /*
  830.  * Begin a keyboard macro.
  831.  * Error if not at the top level in keyboard processing. Set up variables and
  832.  * return.
  833.  */
  834.  
  835. PASCAL NEAR ctlxlp(f, n)
  836.  
  837. int f,n;    /* prefix flag and argument */
  838.  
  839. {
  840.     if (kbdmode != STOP) {
  841.         mlwrite(TEXT105);
  842. /*            "%%Macro already active" */
  843.         return(FALSE);
  844.     }
  845.     mlwrite(TEXT106);
  846. /*        "[Start macro]" */
  847.     kbdptr = &kbdm[0];
  848.     kbdend = kbdptr;
  849.     kbdmode = RECORD;
  850.     return(TRUE);
  851. }
  852.  
  853. /*
  854.  * End keyboard macro. Check for the same limit conditions as the above
  855.  * routine. Set up the variables and return to the caller.
  856.  */
  857.  
  858. PASCAL NEAR ctlxrp(f, n)
  859.  
  860. int f,n;    /* prefix flag and argument */
  861.  
  862. {
  863.     if (kbdmode == STOP) {
  864.         mlwrite(TEXT107);
  865. /*            "%%Macro not active" */
  866.         return(FALSE);
  867.     }
  868.     if (kbdmode == RECORD) {
  869.         mlwrite(TEXT108);
  870. /*            "[End macro]" */
  871.         kbdmode = STOP;
  872.     }
  873.     return(TRUE);
  874. }
  875.  
  876. /*
  877.  * Execute a macro.
  878.  * The command argument is the number of times to loop. Quit as soon as a
  879.  * command gets an error. Return TRUE if all ok, else FALSE.
  880.  */
  881.  
  882. PASCAL NEAR ctlxe(f, n)
  883.  
  884. int f,n;    /* prefix flag and argument */
  885.  
  886. {
  887.     if (kbdmode != STOP) {
  888.         mlwrite(TEXT105);
  889. /*            "%%Macro already active" */
  890.         return(FALSE);
  891.     }
  892.     if (n <= 0)
  893.         return(TRUE);
  894.     kbdrep = n;        /* remember how many times to execute */
  895.     kbdmode = PLAY;     /* start us in play mode */
  896.     kbdptr = &kbdm[0];    /*    at the beginning */
  897.     return(TRUE);
  898. }
  899.  
  900. /*
  901.  * Abort.
  902.  * Beep the beeper. Kill off any keyboard macro, etc., that is in progress.
  903.  * Sometimes called as a routine, to do general aborting of stuff.
  904.  */
  905.  
  906. PASCAL NEAR ctrlg(f, n)
  907.  
  908. int f,n;    /* prefix flag and argument */
  909.  
  910. {
  911.     TTbeep();
  912.     kbdmode = STOP;
  913.     mlwrite(TEXT8);
  914. /*        "[Aborted]" */
  915.     return(ABORT);
  916. }
  917.  
  918. /* tell the user that this command is illegal while we are in
  919.    VIEW (read-only) mode                */
  920.  
  921. PASCAL NEAR rdonly()
  922.  
  923. {
  924.     TTbeep();
  925.     mlwrite(TEXT109);
  926. /*        "[Key illegal in VIEW mode]" */
  927.     return(FALSE);
  928. }
  929.  
  930. PASCAL NEAR resterr()
  931.  
  932. {
  933.     TTbeep();
  934.     mlwrite(TEXT110);
  935. /*        "[That command is RESTRICTED]" */
  936.     return(FALSE);
  937. }
  938.  
  939. PASCAL NEAR nullproc(f, n)    /* user function that does NOTHING */
  940.  
  941. int n, f;    /* yes, these are default and never used.. but MUST be here */
  942.  
  943. {
  944. }
  945.  
  946. PASCAL NEAR meta(f, n)    /* set META prefixing pending */
  947.  
  948. int f, n;    /* prefix flag and argument */
  949.  
  950. {
  951.     prefix |= META;
  952.     prenum = n;
  953.     predef = f;
  954.     return(TRUE);
  955. }
  956.  
  957. PASCAL NEAR cex(f, n)    /* set ^X prefixing pending */
  958.  
  959. int f, n;    /* prefix flag and argument */
  960.  
  961. {
  962.     prefix |= CTLX;
  963.     prenum = n;
  964.     predef = f;
  965.     return(TRUE);
  966. }
  967.  
  968. PASCAL NEAR unarg()    /* dummy function for binding to universal-argument */
  969. {
  970. }
  971.  
  972. /*    bytecopy:    copy a string...with length restrictions
  973.             ALWAYS null terminate
  974. */
  975.  
  976. char *PASCAL NEAR bytecopy(dst, src, maxlen)
  977.  
  978. char *dst;    /* destination of copied string */
  979. char *src;    /* source */
  980. int maxlen;    /* maximum length */
  981.  
  982. {
  983.     char *dptr;    /* ptr into dst */
  984.  
  985.     dptr = dst;
  986.     while (*src && (maxlen-- > 0))
  987.         *dptr++ = *src++;
  988.     *dptr = 0;
  989.     return(dst);
  990. }
  991.  
  992. /*    copystr:    make another copy of the argument
  993.  
  994. */
  995.  
  996. char *PASCAL NEAR copystr(sp)
  997.  
  998. char *sp;    /* string to copy */
  999.  
  1000. {
  1001.     char *dp;    /* copy of string */
  1002.  
  1003.     /* make room! */
  1004.     dp = malloc(strlen(sp)+1);
  1005.     if (dp == NULL)
  1006.         return(NULL);
  1007.     strcpy(dp, sp);
  1008.     return(dp);
  1009. }
  1010.  
  1011. /*****        Compiler specific Library functions    ****/
  1012.  
  1013. #if    RAMSIZE
  1014. /*    These routines will allow me to track memory usage by placing
  1015.     a layer on top of the standard system malloc() and free() calls.
  1016.     with this code defined, the environment variable, $RAM, will
  1017.     report on the number of bytes allocated via malloc.
  1018.  
  1019.     with SHOWRAM defined, the number is also posted on the
  1020.     end of the bottom mode line and is updated whenever it is changed.
  1021. */
  1022.  
  1023. #undef    malloc
  1024. #undef    free
  1025.  
  1026. #if     VMS & OPTMEM        /* these routines are faster! */
  1027. #define    malloc    VAXC$MALLOC_OPT
  1028. #define free    VAXC$FREE_OPT
  1029. #endif
  1030.  
  1031. char *allocate(nbytes)    /* allocate nbytes and track */
  1032.  
  1033. unsigned nbytes;    /* # of bytes to allocate */
  1034.  
  1035. {
  1036.     char *mp;    /* ptr returned from malloc */
  1037.     char *malloc();
  1038.     FILE *track;    /* malloc track file */
  1039.  
  1040.     mp = malloc(nbytes);
  1041.  
  1042. #if    RAMTRCK
  1043.     track = fopen("malloc.dat", "a");
  1044.     fprintf(track, "Allocating %u bytes at %u:%u\n", nbytes,
  1045.             FP_SEG(mp), FP_OFF(mp));
  1046.     fclose(track);
  1047. #endif
  1048.  
  1049.     if (mp) {
  1050. #if    0
  1051.         envram += nbytes;
  1052. #else
  1053.         envram += 1;
  1054. #endif
  1055. #if    RAMSHOW
  1056.         dspram();
  1057. #endif
  1058.     }
  1059.  
  1060.     return(mp);
  1061. }
  1062.  
  1063. release(mp)    /* release malloced memory and track */
  1064.  
  1065. char *mp;    /* chunk of RAM to release */
  1066.  
  1067. {
  1068.     unsigned *lp;    /* ptr to the long containing the block size */
  1069. #if    RAMTRCK
  1070.     FILE *track;    /* malloc track file */
  1071.  
  1072.     track = fopen("malloc.dat", "a");
  1073.     fprintf(track, "Freeing %u:%u\n",
  1074.             FP_SEG(mp), FP_OFF(mp));
  1075.     fclose(track);
  1076. #endif
  1077.  
  1078.     if (mp) {
  1079.         /* update amount of ram currently malloced */
  1080. #if    0
  1081.         lp = ((unsigned *)mp) - 1;
  1082.         envram -= (long)*lp - 2;
  1083. #else
  1084.         envram -= 1;
  1085. #endif
  1086.         free(mp);
  1087. #if    RAMSHOW
  1088.         dspram();
  1089. #endif
  1090.     }
  1091. }
  1092.  
  1093. #if    RAMSHOW
  1094. dspram()    /* display the amount of RAM currently malloced */
  1095.  
  1096. {
  1097.     char mbuf[20];
  1098.     char *sp;
  1099.  
  1100.     TTmove(term.t_nrow - 1, 70);
  1101. #if    COLOR
  1102.     TTforg(7);
  1103.     TTbacg(0);
  1104. #endif
  1105.     sprintf(mbuf, "[%lu]", envram);
  1106.     sp = &mbuf[0];
  1107.     while (*sp)
  1108.         TTputc(*sp++);
  1109.     TTmove(term.t_nrow, 0);
  1110.     movecursor(term.t_nrow, 0);
  1111. }
  1112. #endif
  1113. #endif
  1114.